//
//  RaeRequestParameterConfig.swift
//  77Club
//
//  Created by Apple on 2024/8/26.
//

import Foundation
import CoreTelephony
import CommonCrypto
import Alamofire

class RaeRequestParameterConfig: NSObject {
    
    static let shared = RaeRequestParameterConfig()
    
    /// 服务器地址,必须传
    public var baseUrlStr = "https://zentrinot.com/gateway/"
    /// 请求token,必须传
    public var requestToken: String = ""
    public var userType: String = "10"
    var tStr = ""
    
    public func getURLStr()-> String{
        return self.baseUrlStr + "api/app/v3/?"
    }
    
    public func getURLUpLoadStr()-> String{
        return self.baseUrlStr + "?"
    }
    
    public func getTStr()-> String{
        tStr = "\(Date().toTimeStamp())\(getRandomNumber(from: 101, to: 998))"
        return tStr
    }
    
    public func encrySign() -> String {
        
        guard let uids = UserDefaultsManager.getString(forKey: .token), uids.count > 0  else { return "" }
        let sign = "\(uids)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = md5HexDigest(sign)
        return signMD5
    }
    
    func getRandomNumber(from: Int, to: Int) -> Int {
        return Int.random(in: from...to)
    }
    
    func md5HexDigest(_ input: String) -> String {
        let data = Data(input.utf8)
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        
        data.withUnsafeBytes {
            _ = CC_MD5($0.baseAddress, CC_LONG(data.count), &digest)
        }
        
        return digest.map { String(format: "%02x", $0) }.joined()
    }
}


extension RaeRequestParameterConfig {
    
    func DataToJSONString(data: Data) -> String? {
        
        let JSONString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        return JSONString
    }
    
    func JSONStringToDictionary(jsonString:String) ->NSDictionary{
        
        let jsonData:Data = jsonString.data(using: .utf8)!
        
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
}


extension UIDevice {
    
    static var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        if identifier.contains("iPad") {
            return "iPad, 测试人员或者审核人员"
        }
        switch identifier {
        case "iPhone7,1": return "iPhone 6 Plus"
        case "iPhone7,2": return "iPhone 6"
        case "iPhone8,1": return "iPhone 6s"
        case "iPhone8,2": return "iPhone 6s Plus"
        case "iPhone8,4": return "iPhone SE"
        case "iPhone9,1": return "iPhone 7"
        case "iPhone9,2": return "iPhone 7 Plus"
        case "iPhone9,3": return "iPhone 7"
        case "iPhone9,4": return "iPhone 7 Plus"
        case "iPhone10,1": return "iPhone 8"
        case "iPhone10,2": return "iPhone 8 Plus"
        case "iPhone10,3": return "iPhone X"
        case "iPhone10,4": return "iPhone 8"
        case "iPhone10,5": return "iPhone 8 Plus"
        case "iPhone10,6": return "iPhone X"
        case "iPhone11,8": return "iPhone XR"
        case "iPhone11,2": return "iPhone XS"
        case "iPhone11,4": return "iPhone XS Max"
        case "iPhone11,6": return "iPhone XS Max"
        case "iPhone12,1": return "iPhone 11"
        case "iPhone12,3": return "iPhone 11 Pro"
        case "iPhone12,5": return "iPhone 11 Pro Max"
        case "iPhone13,1": return "iPhone 12 mini"
        case "iPhone13,2": return "iPhone 12"
        case "iPhone13,3": return "iPhone 12 Pro"
        case "iPhone13,4": return "iPhone 12 Pro Max"
        case "iPhone14,2": return "iPhone 13 Pro"
        case "iPhone14,3": return "iPhone 13 Pro Max"
        case "iPhone14,4": return "iPhone 13 Mini"
        case "iPhone14,5": return "iPhone 13"
        
        case "iPad13,9": return "iPad Pro 12.9 inch 5th Gen"
        case "iPad13,10": return "iPad Pro 12.9 inch 5th Gen"
        case "iPad13,11": return "iPad Pro 12.9 inch 5th Gen"
        case "iPad14,1": return "iPad mini (6th generation) Wi-Fi"
        case "iPad14,2": return "iPad mini (6th generation) Wi-Fi + Cellular"
        
        case "i386": return "Simulator"
        case "x86_64": return "Simulator"
        default: return ""
        }
    }
}
